package controller;

import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class registerServlet
 */
@WebServlet("/registerServlet")
public class registerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    @EJB
    Biz.userBeanLocal _userBeanLocal;
    public registerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8"); 
		String user_ID=request.getParameter("user_ID"),
				password=request.getParameter("password"),
				name=request.getParameter("name");
		if(_userBeanLocal.register(user_ID,password,name))
		{
			System.out.println(name);
			request.setAttribute("_userBeanLocal", _userBeanLocal);
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
		else
		{
			response.getOutputStream().println("ID is already used");
			response.setHeader("refresh","2;URL=/register.jsp");
		}
	}

}
